<template>
  <div>
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" :error.sync="error"
      error-text="请求失败，点击重新加载">
      <commentItem @updataCommentLiking="item.is_liking = $event" :comment="item" v-for="item in list" :key="item.com_id + ''"/>
      <!-- <van-cell v-for="item in list" :key="item.com_id + ''" :title="item.content" /> -->
    </van-list>
  </div>
</template>

<script>
import { getUserChannles } from '@/api/comment.js'
import commentItem from '@/components/sg-ui/comment-item.vue'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      offset: null,
      limit: 10,
      totalCount: 0,
      error: false
    }
  },
  props: {
    type: {
      type: String,
      default: 'a'
    },
    source: {
      type: [Number, Object],
      required: true
    }
  },
  methods: {
    async onLoad () {
      // 1.发送请求获取数据
      try {
        const { data: res } = await getUserChannles({
          type: this.type,
          source: this.source,
          offset: this.offset,
          limit: this.limit
        })
        console.log(res)
        // 2.保存数据
        this.list.push(...res.data.results)
        this.totalCount = res.data.total_count
        this.$emit('updataTotal', this.totalCount)
        // 3.加载状态结束
        this.loading = false
        // 4.数据全部加载完成
        if (this.list.length === this.totalCount) {
          this.finished = true
        } else {
          this.offset = res.data.last_id
        }
      } catch (err) {
        this.loading = false
        this.error = true
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {
    commentItem
  },
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
